const path = require('path');
const {merge} = require('webpack-merge');
const webpack = require('webpack');
const commonWebpackConfig = require('./webpack.common');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
function compareTo10(num) {
    if(num < 10){
        num = "0" + num
    }
    return num;
}
function formatDate(now) {
    let year=now.getFullYear();
    let month=now.getMonth()+1;
    let date=now.getDate();
    let hour=now.getHours();
    let minute=now.getMinutes();
    let second=now.getSeconds();
    return year + compareTo10(month) + compareTo10(date) + compareTo10(hour) + compareTo10(minute) + compareTo10(second);
}
const Version = formatDate(new Date());

let config =  merge(commonWebpackConfig,{
    mode: "production",
    module: {
        rules: [
            {
                test: /\.less$/,
                // 使用css-loader和sass-loader解析.scss
                use:[MiniCssExtractPlugin.loader, 'css-loader','less-loader']
            },
            {
                test: /\.css$/,
                // 要加上style-loader才能正确解析.vue文件里的<style>标签内容
                use: [MiniCssExtractPlugin.loader, 'css-loader']
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new webpack.DefinePlugin({
            STATE: JSON.stringify('production')
        }),
        new MiniCssExtractPlugin({
            filename: './static/css/[name].[hash:8].'+Version+'.css',
            chunkFilename: './static/css/[name].[hash:8].'+Version+'.css'
        }),
    ]
});

module.exports = config;